﻿using Newtonsoft.Json;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using CatClient;
using PsCat.Aspet;

namespace King.Box.Utils.Utils
{
    public class HttpClientUtil
    {
        [RequestCat]
        public static string Post(string url, string content, string contentType, string accept, Dictionary<string, string> headers)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Timeout = 1000 * 60 * 30;
            request.Method = HttpMethod.Post.Method;
            request.Headers["Accept-Language"] = "zh-cn";
            request.Headers["Accept-Encoding"] = "gzip,deflate";
            request.Accept = string.IsNullOrEmpty(accept) ? "application/json" : accept;
            request.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
            request.ContentType = string.IsNullOrEmpty(contentType) ? "application/x-www-form-urlencoded" : contentType;
            request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/534.7 (KHTML, like Gecko) Chrome/7.0.517.41 Safari/534.7";

            if (headers != null && headers.Count > 0)
            {
                foreach (KeyValuePair<string, string> pair in headers)
                {
                    request.Headers.Add(pair.Key, pair.Value);
                }
            }

            if (!string.IsNullOrEmpty(content))
            {
                byte[] buffer = Encoding.UTF8.GetBytes(content);
                request.ContentLength = buffer.Length;

                using (Stream requestStream = request.GetRequestStream())
                {
                    requestStream.Write(buffer, 0, buffer.Length);
                }
            }
            else
                request.ContentLength = 0;

            string str = string.Empty;
            var response = (HttpWebResponse)request.GetResponse();
            using (var reader = new StreamReader(response.GetResponseStream()))
            {
                str = reader.ReadToEnd();
            };
            return str;
        }

        [RequestCat]
        public static string Get(string url, string contentType, string accept, Dictionary<string, string> headers)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Timeout = 1000 * 60 * 30;
            request.Method = HttpMethod.Get.Method;
            request.Headers["Accept-Language"] = "zh-cn";
            request.Headers["Accept-Encoding"] = "gzip,deflate";
            request.Accept = string.IsNullOrEmpty(accept) ? "application/json" : accept;
            request.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
            request.ContentType = string.IsNullOrEmpty(contentType) ? "application/x-www-form-urlencoded" : contentType;
            request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/534.7 (KHTML, like Gecko) Chrome/7.0.517.41 Safari/534.7";

            if (headers != null && headers.Count > 0)
            {
                foreach (KeyValuePair<string, string> pair in headers)
                {
                    request.Headers.Add(pair.Key, pair.Value);
                }
            }

            string str = string.Empty;
            var response = (HttpWebResponse)request.GetResponse();
            using (var reader = new StreamReader(response.GetResponseStream()))
            {
                str = reader.ReadToEnd();
            };
            return str;
        }
    }
}
